from pydantic_settings import BaseSettings
from functools import lru_cache

class Settings(BaseSettings):
    # 服务配置
    HOST: str = "0.0.0.0"
    PORT: int = 8000
    
    # 文档配置
    DOCS_PATH: str = "data/documents"
    VECTOR_STORE_PATH: str = "data/vector_store"
    
    # RAG配置
    LLM_MODEL: str = "llama3.1"
    LLM_TEMPERATURE: float = 0.7
    RETRIEVER_TOP_K: int = 4
    
    # 性能优化配置
    USE_CUDA: bool = True
    BATCH_SIZE: int = 32
    CACHE_DIR: str = "models/cache"
    PERSIST_VECTORS: bool = True
    
    # 文档处理配置
    MAX_DOCUMENTS: int = 1000
    CHUNK_SIZE: int = 1000
    
    # OpenAI配置
    OPENAI_API_KEY: str
    OPENAI_BASE_URL: str = "https://api.openai.com/v1"
    
    # 模型加载优化配置
    MODEL_CACHE_SIZE: int = 1024  # 缓存大小(MB)
    MODEL_PRELOAD: bool = True    # 是否预加载
    MODEL_QUANTIZE: bool = True   # 是否量化
    MODEL_BATCH_SIZE: int = 32    # 批处理大小
    
    # 性能监控配置
    ENABLE_PROFILING: bool = False  # 是否启用性能分析
    
    # Moonshot配置
    MOONSHOT_API_KEY: str = ""
    MOONSHOT_MODEL: str = "moonshot-v1-8k"
    
    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = True

@lru_cache()
def get_settings() -> Settings:
    return Settings()
